package prototype;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-26 19:44
 * @notify
 * @version 1.0
 */

import junit.framework.TestCase;
import org.junit.Test;

public class PrototypeTest extends TestCase {

    @Test
    public void testShallowCopy1() throws Exception{
        Address address=new Address();
        address.setType("Home");
        address.setValue("北京");

        Person p1=new Person();
        p1.setAge(31);
        p1.setName("Peter");
        p1.setAddress(address);

        Person p2=(Person) p1.clone();
        System.out.println(p1==p2);//false

        p2.setName("张三");
        p2.getAddress().setType("Office");
        System.out.println("p1="+p1);
        System.out.println("p2="+p2);
    }


    @Test
    public void testShallowCopy2() throws Exception{
        Address address=new Address();
        address.setType("Home");
        address.setValue("北京");

        Person p1=new Person();
        p1.setAge(31);
        p1.setName("Peter");
        p1.setAddress(address);

        Person p2=(Person) p1.deepClone();
        System.out.println(p1==p2);//false

        p2.setName("张三");
        p2.getAddress().setType("Office");
        System.out.println("p1="+p1);
        System.out.println("p2="+p2);
    }
}
